package com.songcha.module_splash.ui.activity.splash

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_business.bean.advert.AdvertInfoBean
import com.songcha.library_business.bean.configuration.SystemConfigurationResultBean
import com.songcha.library_business.bean.user.UserInfoBean
import com.songcha.library_business.config.FunctionConfig
import com.songcha.library_business.config.VarConfig
import com.songcha.library_business.proxy.configuration.SystemConfigurationManager
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_business.viewmodel.BaseErrorViewModel
import com.songcha.library_common.util.DeviceUtil
import com.songcha.library_network.bean.BaseBean
import com.songcha.library_network.bean.BaseStringDataBean

class SplashViewModel(app:Application):BaseErrorViewModel<SplashRepository>(app) {
    //var isAgreeUserPolicy=0

    val skipCountDown = MutableLiveData<Int>()
    val advertState = MutableLiveData<Boolean>()
    val systemConfigurationState=MutableLiveData<Boolean>()

    init {
        repository= SplashRepository()
    }

    fun appStart(){
        handleApiDataObserver(repository!!.appStart(),object:IApiResult<BaseBean>{
            override fun onSuccess(bean: BaseBean) {
                updateClientIp()
                loginByDevice()
            }

        },false,false,false)
    }

    fun updateClientIp(){
        handleApiDataObserver(repository!!.updateClientIp(),object:IApiResult<BaseBean>{
            override fun onSuccess(bean: BaseBean) {

            }

        },false,false,false)
    }

    fun loginByDevice(){
        if(UserInfoManager.loginToken.isNotBlank()) return
        handleApiDataObserver(repository!!.loginByDeviceId(DeviceUtil.getAndroidId(getApplication())),object:IApiResult<BaseStringDataBean>{
            override fun onSuccess(bean: BaseStringDataBean) {
                UserInfoManager.saveLogin(bean.data)
            }

        },false,false,false)
    }

    fun firstLoginByDevice(){
        handleApiDataObserver(repository!!.loginByDeviceId(DeviceUtil.getAndroidId(getApplication())),object:IApiResult<BaseStringDataBean>{
            override fun onSuccess(bean: BaseStringDataBean) {
                UserInfoManager.saveLogin(bean.data)
                appStart()
            }

            override fun onError(e: Throwable) {
                super.onError(e)
            }

        },false,false,false)
    }

    fun getSystemConfiguration(){
        handleApiDataObserver(SystemConfigurationManager.getSystemConfiguration(),object:IApiResult<SystemConfigurationResultBean>{
            override fun onSuccess(bean: SystemConfigurationResultBean) {
                advertState.value = bean.advertState && VarConfig.ADVERT_SPLASH_INFO.type != AdvertInfoBean.ADVERT_TYPE_DIS_ENABLED
                systemConfigurationState.value = true
            }

            override fun onError(e: Throwable) {
                advertState.value = false
                systemConfigurationState.value = false
            }

        },false,false,false)
    }


    fun setCountDown(countDown:Int){
        skipCountDown.value=countDown
    }
}